package cn.mntool.custom.config;

import cn.mntool.custom.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * describe: 自动配置类
 *
 * @author lianying
 * @date 2020/07/17
 */
@Configuration
@ConditionalOnClass(TestService.class)
@EnableConfigurationProperties(CustomProperties.class)
public class CustomAutoConfiguration {
    @Autowired
    private CustomProperties customProperties;

    @Bean
//    @ConditionalOnProperty(name = "custom.enabled",havingValue = "true")//    这个方法不好用，idea不进行提示
    public TestService testService(){
        return customProperties.isEnabled()?new TestService(customProperties.getPrefix(),customProperties.getSuffix()):new TestService("","");
    }

// 保留写法二
//    @Bean
//    @ConditionalOnMissingBean
//    public TestService testService2(){
//        return new TestService("","");
//    }
}
